package com.qf.servlet;

import com.qf.entity.Coupon;
import com.qf.service.CouponService;
import com.qf.service.impl.CouponServiceImpl;
import com.qf.utils.JsonUtils;
import com.qf.utils.PageUtils;
import com.qf.utils.ResultVo;

import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;

@WebServlet(name = "couponServlet", urlPatterns = "/couponServlet")
public class CouponServlet extends BaseServlet {
    private CouponService couponService = new CouponServiceImpl();
    /**
     * 添加优惠券
     *
     * @param req
     * @param resp
     * @throws IOException
     */
    public void addCoupon(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        //获取输出流对象
        PrintWriter pw = resp.getWriter();
        String cou_name = req.getParameter("cou_name");
        String cou_remark = req.getParameter("cou_remark");
        String cou_content = req.getParameter("cou_content");
        //实例化一个Coupon对象
        Coupon coupon = new Coupon();
        coupon.setCou_name(cou_name);
        coupon.setCou_remark(cou_remark);
        coupon.setCou_content(cou_content);
        //调用service层方法
        int num = couponService.addCoupon(coupon);
        //实例化结果对象
        ResultVo<Boolean> resultVo = new ResultVo<>();
        if (num > 0) {
            resultVo.setCode(200);
            resultVo.setMessage("添加成功");
            resultVo.setData(true);

        } else {
            resultVo.setCode(500);
            resultVo.setMessage("添加失败");
            resultVo.setData(false);

        }
        JsonUtils.showJson(pw, resultVo);

    }

    /**
     * 展示优惠券
     *
     * @param req
     * @param resp
     * @throws IOException
     */
    public void showCoupon(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        //获取输出流
        PrintWriter pw = resp.getWriter();
        //获取前端传来的参数
        String currentPageNo = req.getParameter("currentPageNo");
        String pageSize = req.getParameter("pageSize");
        //调用Service层方法
        PageUtils<Coupon> couponPageUtils = couponService.getCouponPageUtils(currentPageNo, pageSize);
        //实例化一个结果对象
        ResultVo<PageUtils<Coupon>> resultVo = new ResultVo<>();
        //判断数据是否为null
        if (couponPageUtils != null) {
            resultVo.setCode(200);
            resultVo.setData(couponPageUtils);
            resultVo.setMessage("显示成功");
        } else {
            resultVo.setCode(500);
            resultVo.setMessage("显示失败");
            resultVo.setData(null);
        }
        JsonUtils.showJson(pw, resultVo);
    }

    /**
     * 删除优惠券
     *
     * @param req
     * @param resp
     * @throws IOException
     */
    public void deleteCoupon(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        //获取输出流对象
        PrintWriter pw = resp.getWriter();
        //实例化结果对象
        ResultVo<Boolean> resultVo = new ResultVo<>();
        //获取前端传来的数据
        String cidStr = req.getParameter("cid");
        Integer cid = null;
        //调用service方法
        if (cidStr != null && !"".equals(cidStr)) {
            cid = Integer.parseInt(cidStr);
        }
        int num = couponService.deleteC_vAndCouponByCid(cid);
        if (num > 0) {
            resultVo.setCode(200);
            resultVo.setMessage("删除成功");
            resultVo.setData(true);
        } else {
            resultVo.setCode(500);
            resultVo.setMessage("删除失败");
            resultVo.setData(false);
        }
        JsonUtils.showJson(pw, resultVo);
    }

}
